$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_fblx_m")
    const dgrid1 = $NG.getCmpApi("p_form_fblx_d2")
    const dgrid2 = $NG.getCmpApi("p_form_fblx_d1")
    const getHeadValue = (str) => mstform.getItem(str).getValue()
    const config = {
        list_data_bak: []

    }
    GctsFilter()
    if (page.isList) {
        setInterval(() => {
            let bigBox = $NG.getCmpApi("grid")?.getRef()?.bodyRef.current
            const start = new Date().getTime()
            let dom = bigBox.childNodes[0].childNodes[0]
            // while(new Date().getTime() - start < 10 * 1000 && !dom){
            //     dom = document.childNodes[0].childNodes[0]
            // }
            // if(!dom){
            //     $NG.alert("发生异常！")
            // }
            if (!dom) {
                return
            }
            dom.childNodes.forEach(el => {
                let colorLock = false
                // console.log(`el`,el);
                el.childNodes.forEach(it => {
                    if (it.getAttribute("data-key") == 'u_ykqk') {
                        const minDom = it.childNodes[0]?.textContent
                        if ((Number(minDom) || 0) < 0) {
                            colorLock = true
                        }
                    }
                })
                if (colorLock) {
                    el.childNodes.forEach(item => {
                        item.style.background = '#ff000038'
                    })
                }
            })
        }, 1000)
    }


    useUpdateRows(() => {
        const rows = dgrid1.getRows()
        let total_amt = 0
        let total_amt_cb = 0
        rows.forEach(el => {
            total_amt += (Number(el.u_yfb1) || 0)
            total_amt_cb += (Number(el.u_dydcbhtje) || 0)
            el.u_dydcbhtje = (Number(el.u_gcl) || 0) * (Number(el.u_dj) || 0)
            el.u_yfb1 = (Number(el.u_nfb) || 0) * (Number(el.u_gcl) || 0)
            el.u_lr1 = (Number(el.u_dydcbhtje) || 0) - (Number(el.u_yfb1) || 0) - (Number(el.u_fyxj) || 0)
            el.u_lr = (Number(el.u_lr1) || 0) / (Number(el.u_gcl) || 0)
            el.u_cl1 = (Number(el.u_cl) || 0) * (Number(el.u_gcl) || 0)
            el.u_jx1 = (Number(el.u_jx) || 0) * (Number(el.u_gcl) || 0)
            el.u_qtfy1 = (Number(el.u_qtfy) || 0) * (Number(el.u_gcl) || 0)
            el.u_sj1 = (Number(el.u_sj) || 0) * (Number(el.u_gcl) || 0)
        })
        mstform.getItem("u_yjfbzje").setValue((Number((total_amt / 10000).toFixed(4)) || 0))
        mstform.getItem("u_dycbhtje").setValue((Number((total_amt_cb / 10000).toFixed(4)) || 0))
        mstform.getItem("u_ykqk").setValue(total_amt_cb - total_amt)
        if (total_amt < 10000 * 1000) {
            mstform.getItem("u_gjfbgcjk").setValue("不超过1000万元")
        } else {
            mstform.getItem("u_gjfbgcjk").setValue("1000万元以上")
        }
        dgrid1.refreshView()
    }, "p_form_fblx_d2")

    useValuesChange(() => {
        GctsFilter()
                getFbchDetail2()
         getFbchDetail1()
    }, "u_fbxmchsbh")


    function getFbchDetail1() {
        const phid = mstform.getItem("u_fbxmchsbh").getValue()
        if (!phid) return
        $NG.execServer("SELECT",{sql:`select d.rel_key1,d.u_fbxmmc from p_form_fbch_d2 d left join p_form_fbch_m m on m.phid=d.pphid where m.phid = '${phid}'`},(res)=>{
            if(res.count == 0)return 
            const datas = JSON.parse(res.data).reduce((value,item)=>{
                const it = item.extendObjects
                value.push({
                    ...it,
                    u_fblxmc:it.u_fbxmmc
                })
                return value
            },[])
            dgrid2.clearRows().then(()=>{
                dgrid2.addRows(datas)
            })
        })
    }

    function getFbchDetail2() {
        const phid = mstform.getItem("u_fbxmchsbh").getValue()
        if (!phid) return
        $NG.execServer("getFbchDetail2", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data).reduce((value, item) => {
                const it = item.extendObjects
                value.push({
                    ...it,
                    u_cl1: it.u_clje,
                    u_lr1: it.u_lrje,
                    u_yfb1: it.u_nfb,
                    u_dydcbhtje: it.u_dydcbhtje,
                    u_jx1: it.jxje,
                    u_qtfy1: it.u_qtfyje,
                    u_sj1: it.u_sjje,
                    u_fyxj1: itgetFblxInfoByChPhid.u_fyxjje,
                    u_bz: it.u_bz
                })
                return value
            }, [])
            let arr = arrayToTree(datas, "s_tree_id", "s_tree_pid")
            dgrid1.clearRows().then(() => {
                dgrid1.addRows(arr)
            })
        })
    }
    // 转属性结构
    function arrayToTree(items, idField = "id", parentField = "parentId", childrenField = "children") {
        const map = {};
        const roots = [];

        // 先建立 id → item 的映射
        items.forEach(item => {
            map[item[idField]] = { ...item, [childrenField]: [] };
        });

        // 组装树
        items.forEach(item => {
            const parentId = item[parentField];
            if (parentId == null || parentId.length < 2 || parentField == '0') {
                // 根节点
                roots.push(map[item[idField]]);
            } else if (map[parentId]) {
                // 挂到父节点的 children 上
                map[parentId][childrenField].push(map[item[idField]]);
            }
        });

        return roots;
    }
    function GctsFilter() {
        try {
            mstform.getItem("u_gctssxbh").setProps({
                onBeforeOpen: async () => {
                    var u_fbxmchsbh = getHeadValue("u_fbxmchsbh")
                    if (!u_fbxmchsbh) {
                        $NG.alert("请选择分包项目策划书编号！");
                        return false;
                    } else {
                        return true;
                    }
                },
                clientSqlFilter: {
                    phid: getHeadValue("u_fbxmchsbh"),
                }
            });
        } catch (err) {
            console.log(`错误`, err);
        }
    }
})